package com.sxjh.controller.ny;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxjh.annotation.UserLog;
import com.sxjh.common.BaseController;
import com.sxjh.common.Result;
import com.sxjh.common.ZPage;
import com.sxjh.entity.FactoryEntity;
import com.sxjh.entity.WorkshopEntity;
import com.sxjh.entity.param.FactoryAddParam;
import com.sxjh.entity.param.FactoryListParam;
import com.sxjh.enums.DelFlagEnum;
import com.sxjh.service.FactoryService;
import com.sxjh.service.WorkshopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 内部倒运-工厂管理 前端控制器
 * </p>
 *
 * @author guanxy
 * @since 2023-06-02
 */
@Api(tags = "工厂管理")
@RestController
@RequestMapping("/factory")
public class FactoryController extends BaseController {


    @Autowired
    private FactoryService factoryService;

    @Autowired
    private WorkshopService workshopService;

    @ApiOperation("分页列表")
    @GetMapping(value = "/factory.list")
    public Result<ZPage<FactoryListParam>> list(FactoryListParam param) {
        LambdaQueryWrapper<FactoryEntity> queryWrapper = new LambdaQueryWrapper();
        if (StrUtil.isNotBlank(param.getName())) {
            queryWrapper.like(FactoryEntity::getName, param.getName());
        }
        if (param.getId() != null) {
            queryWrapper.eq(FactoryEntity::getId, param.getId());
        }
        if (param.getDelFlag() != null) {
            queryWrapper.eq(FactoryEntity::getDelFlag, DelFlagEnum.NO.getType());
        }
        queryWrapper.orderByDesc(FactoryEntity::getId);
        Page<FactoryEntity> aPage = factoryService.page(new Page<>(param.getCurrent(), param.getSize()), queryWrapper);
        ZPage<FactoryListParam> zPage = copyPage(aPage, FactoryListParam::new);
        if (zPage != null && CollUtil.isNotEmpty(zPage.getRecords())) {
            zPage.getRecords().forEach(p -> {
                if (StrUtil.isNotBlank(p.getRoleId())) {
                    List<String> list = Arrays.asList(p.getRoleId().split(","));
                    p.setRoleIds(list.stream().map(Integer::valueOf).collect(Collectors.toList()));
                }
                if (StrUtil.isNotBlank(p.getRoleName())) {
                    List<String> list = Arrays.asList(p.getRoleName().split(","));
                    p.setRoleNames(list.stream().map(String::valueOf).collect(Collectors.toList()));
                }
            });
        }
        return Result.ok(zPage);
    }

    @ApiOperation("查询")
    @GetMapping(value = "/{id}")
    public Result<FactoryEntity> getById(@PathVariable("id") String id) {
        return Result.ok(factoryService.getById(id));
    }

    @ApiOperation("新增")
    @PostMapping(value = "/add")
    @UserLog(desc = "内部倒运-新增工厂", roleCode = "nbdy.factory.add")
    public Result<Object> create(@RequestBody FactoryAddParam params) {
        FactoryEntity params1 = new FactoryEntity();
        BeanUtil.copyProperties(params, params1);
        if (CollUtil.isNotEmpty(params.getRoleIds())) {
            params1.setRoleId(params.getRoleIds().stream().map(String::valueOf).collect(Collectors.joining(",")));
        }
        if (CollUtil.isNotEmpty(params.getRoleNames())) {
            params1.setRoleName(params.getRoleNames().stream().map(String::valueOf).collect(Collectors.joining(",")));
        }
        factoryService.save(params1);
        return Result.ok(HttpStatus.OK);
    }

    @ApiOperation("删除")
    @GetMapping(value = "/delete/{id}")
    @UserLog(desc = "内部倒运-删除工厂", roleCode = "nbdy.factory.del")
    public Result<Object> delete(@PathVariable("id") String id) {
        int count = workshopService.count(new LambdaQueryWrapper<WorkshopEntity>().eq(WorkshopEntity::getFactoryId, id));
        if (count > 0) return Result.err("该工厂在车间管理中被使用了!");
        FactoryEntity params = new FactoryEntity();
        params.setId(Integer.valueOf(id));
        params.setDelFlag(DelFlagEnum.YES.getType());
        factoryService.updateById(params);
        return Result.ok(HttpStatus.OK);
    }

    @ApiOperation("修改")
    @PostMapping(value = "/update")
    @UserLog(desc = "内部倒运-修改工厂", roleCode = "nbdy.factory.update")
    public Result<Object> update(@RequestBody FactoryAddParam params) {
        FactoryEntity params1 = new FactoryEntity();
        BeanUtil.copyProperties(params, params1);
        if (CollUtil.isNotEmpty(params.getRoleIds())) {
            params1.setRoleId(params.getRoleIds().stream().map(String::valueOf).collect(Collectors.joining(",")));
        } else {
            params1.setRoleId("");
        }
        if (CollUtil.isNotEmpty(params.getRoleNames())) {
            params1.setRoleName(params.getRoleNames().stream().map(String::valueOf).collect(Collectors.joining(",")));
        }
        factoryService.updateById(params1);
        return Result.ok(HttpStatus.OK);
    }
}
